import { createRouter, createWebHistory } from "vue-router";
import { isLogin } from "@/utils/userInfo";
import store from "@/store";
const routes = [
  {
    path: "/home",
    component: () => import("@/view/HomePage/index.vue"),
  },
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/hospital",
    component: () => import("@/view/HospitalPage/index.vue"),
    children: [
      {
        path: "/hospital",
        redirect: "/hospital/registerPage",
      },
      {
        path: "registerPage",
        component: () =>
          import("@/view/HospitalPage/components/registerPage/index.vue"),
      },
      {
        path: "detailPage",
        component: () =>
          import("@/view/HospitalPage/components/detailPage/index.vue"),
      },
      {
        path: "noticePage",
        component: () =>
          import("@/view/HospitalPage/components/noticePage/index.vue"),
      },
      {
        path: "closePage",
        component: () =>
          import("@/view/HospitalPage/components/closePage/index.vue"),
      },
      {
        path: "searchPage",
        component: () =>
          import("@/view/HospitalPage/components/searchPage/index.vue"),
      },
      {
        path: "register_step1",
        component: () =>
          import(
            "@/view/HospitalPage/components/registerPage/components/register_step1.vue"
          ),
      },
      {
        path: "register_step2",
        component: () =>
          import(
            "@/view/HospitalPage/components/registerPage/components/register_step2.vue"
          ),
      },
    ],
  },
  {
    path: "/wxlogin",
    component: () => import("@/view/wxlogin/index.vue"),
  },
  {
    path: "/user",
    component: () => import("@/view/UserPage/index.vue"),
    children: [
      {
        path: "/user",
        redirect: "/user/cetificationPage",
      },
      {
        path: "cetificationPage",
        component: () =>
          import("@/view/UserPage/components/cetificationPage/index.vue"),
      },
      {
        path: "orderPage",
        component: () =>
          import("@/view/UserPage/components/orderPage/index.vue"),
      },
      {
        path: "patientPage",
        component: () =>
          import("@/view/UserPage/components/patientPage/index.vue"),
      },
      {
        path: "profilePage",
        component: () =>
          import("@/view/UserPage/components/profilePage/index.vue"),
      },
      {
        path: "feedbackPage",
        component: () =>
          import("@/view/UserPage/components/feedbackPage/index.vue"),
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
  scrollBehavior() {
    return {
      left: 0,
      top: 0,
    };
  },
});

//存储用户未登录可以访问路由得路径
let whiteList = [
  "/home",
  "/hospital/registerPage",
  "/hospital/detailPage",
  "/hospital/noticePage",
  "/hospital/closePage",
  "/hospital/searchPage",
];
// 添加路由前置守卫
router.beforeEach((to, from, next) => {
  if (isLogin()) {
    next();
  } else {
    if (whiteList.includes(to.path)) {
      next();
    } else {
      store.commit("user/GET_DIALOG", true);
      next({ path: "/home", query: { redirect: to.fullPath } });
    }
  }
});
export default router;
